package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.User;
import com.net.tkc.pro.service.UserService;
import com.net.tkc.pro.tool.*;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/User/")
public class UserController {
    ResultEntity resultEntity = null;
    @Resource
    UserService userService;

    /**
     * 根据用户id来更改除id之外的东西
     * @param uname 用户名
     * @param email 邮箱
     * @param upwd 密码
     * @param phone 手机号
     * @param province 省份
     * @param city 城市
     * @param county 区县
     * @param cardid 身份证
     * @param avatar 头像路径
     * @param username 用户真实姓名
     * @param sex 性别
     * @param autograph 个性签名
     * @param online 是否在线
     * @param age 年龄
     * @param type 是否为普通用户
     * @param islive 是否在直播
     * @param isvolunteer 是否是志愿者
     * @param status 账号是否封禁
     * @param id 用户uid
     * @return
     */
    @PutMapping("User")
    private String upUser(
            @RequestParam("uname") String uname,
            @RequestParam("email") String email,
            @RequestParam("upwd") String upwd,
            @RequestParam("phone") String phone,
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam("county") String county,
            @RequestParam("cardid") String cardid,
            @RequestParam("avatar") String avatar,
            @RequestParam("username") String username,
            @RequestParam("sex") String  sex,
            @RequestParam("autograph") String autograph,
            @RequestParam("online") int online,
            @RequestParam("age") int age,
            @RequestParam("type") int type,
            @RequestParam("islive") int islive,
            @RequestParam("isvolunteer") int isvolunteer,
            @RequestParam("status") int status,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = userService.upUser(uname,email,upwd,phone,province,city,county,cardid,avatar,username,sex,autograph,online
                    ,age,type,islive,isvolunteer,status,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    @PutMapping("User2")
    private String upUser2(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = userService.upUser2(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @DeleteMapping("User")
    private String upUser(
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = userService.delUser(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     *  判读是否重复注册手机号 如果没有就注册进去 如果有就返回该手机号已被使用
     * @param phone 手机号
     * @param upwd  密码
     * @return json
     */
    @PostMapping("User")
    private String addUser(
            @RequestParam("phone") String phone,
            @RequestParam("upwd") String upwd
    ){
        resultEntity = new ResultEntity();
        User user=new User();
        user.setPhone(phone);
        user.setUpwd(upwd);

        if (phone==null && phone.equals("") && upwd==null && upwd.equals("")){
            return "手机号或密码为空";
        }else if (getUser2(phone).charAt(13)=='0'){
            return "该手记号已被使用";
        } else {
            try{
                int result = userService.addUser(user);
                if(result>0){
                    resultEntity.setCode("0");
                    resultEntity.setMsg("添加成功！");
                }else{
                    resultEntity.setCode("1");
                    resultEntity.setMsg("添加失败！");
                }
            }catch (Exception e){
                e.printStackTrace();
                resultEntity.setCode("9999");
                resultEntity.setMsg(e.getMessage());
            }
            return JsonUtil.getJsonTool().toJson(resultEntity);
        }
    }
    /**
     *  无条件查询用户
     */
    @GetMapping("User3")
    public String getUser3(
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            List<User> user= userService.getUser3();
            if (user != null && !user.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(user);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据手机号查找用户 也可以通过短信登录来用手机号获取用户信息
     * @param phone 手机号
     * @return json
     */
    @GetMapping("User4")
    public String getUser4(
            @RequestParam("phone") String phone
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            List<User> user= userService.getUser4(phone);
            if (user != null && !user.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(user);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  根据手机号查找用户 也可以通过短信登录来用手机号获取用户信息
     * @param phone 手机号
     * @return json
     */
    @GetMapping("User2")
    public String getUser2(
            @RequestParam("phone") String phone
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            User user= userService.getUser2(phone);
            if (user != null && !user.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(user);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 通过账号密码来登录app
     * @param phone 手机号
     * @param upwd 密码
     * @return json
     */
    @GetMapping("User")
    public String getUser(
            @RequestParam("phone") String phone,
            @RequestParam("upwd") String upwd
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            User user= userService.getUser(phone,upwd);
            if (user != null && !user.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(user);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("手机号或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 生成base64格式的二维码
     * @return
     */

    @GetMapping("API2")
    public String getAPI2(
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            String flag= API2Tool.Get();
            if (flag != null && !flag.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("二维码生成成功");
                resultEntity.setData(flag);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("生成失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 根据页码获取新闻头条
     * @param page 页码
     * @return
     */
    @GetMapping("API3")
    public String getAPI3(
            @RequestParam("page") int page
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            JSONArray flag= API3Tool.NewGet(page);
            if (flag!=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(flag);
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @GetMapping("API")
    public String getAPI(
            @RequestParam("question") String question
    ){resultEntity = new ResultEntity();
        resultEntity = new ResultEntity();
        try {
            String flag= APITool.Get(question);
            if (flag != null && !flag.equals("")) {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(flag);
            } else {
                resultEntity.setCode("1");
                resultEntity.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    @PostMapping("Img")
    public String Img (
            @RequestParam("file") MultipartFile[] file
    ){
        String[] res=new String[file.length];
        for (int i=0;i<=file.length;i++){
            resultEntity = new ResultEntity();
            String SuffixName=file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf("."));
            String FileNewName= UUID.randomUUID().toString()+SuffixName;
            File dest=new File("C:\\fzapi\\Img\\"+FileNewName);
            if (!dest.getParentFile().exists()){
                dest.getParentFile().mkdir();
            }
            try{
                file[i].transferTo(dest);
            }catch (Exception e){
                e.printStackTrace();
            }

            OSSUtil ossUtil=new OSSUtil();
            if (ossUtil.updateFile("C:\\fzapi\\Img\\"+FileNewName,FileNewName)){
                res[i]="http://img.lfpff.xyz/"+FileNewName;
            }
        }
            resultEntity.setCode(0);
            resultEntity.setData(res);
            return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
